//顺序搜索
function sequentialSearch(arr, target) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === target) {
            return i; // 返回目标元素的索引
        }
    }
    return -1; // 如果未找到目标元素，则返回-1
}

// 例子
let myArray = [4, 2, 7, 1, 9];
let targetElement = 7;
let result = sequentialSearch(myArray, targetElement);
console.log("目标元素的索引是：" + result);
//二分搜索，，，，，
function binarySearch(arr, target) {
    let left = 0;
    let right = arr.length - 1;

    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) {
            return mid; // 返回目标元素的索引
        } else if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1; // 如果未找到目标元素，则返回-1
}

// 例子
let myArray1 = [1, 3, 5, 7, 9, 11, 13, 15];
let targetElement1 = 7;
let result1 = binarySearch(myArray1, targetElement1);
console.log("目标元素的索引是：" + result1);
//内插搜索
function interpolationSearch(arr, target) {
    let low = 0;
    let high = arr.length - 1;

    while (low <= high && target >= arr[low] && target <= arr[high]) {
        let pos = low + Math.floor(((target - arr[low]) * (high - low)) / (arr[high] - arr[low]));

        if (arr[pos] === target) {
            return pos; // 返回目标元素的索引
        } else if (arr[pos] < target) {
            low = pos + 1;
        } else {
            high = pos - 1;
        }
    }
    return -1; // 如果未找到目标元素，则返回-1
}

// 例子
let myArray2 = [1, 3, 5, 7, 9, 11, 13, 15];
let targetElement2 = 7;
let result2 = interpolationSearch(myArray2, targetElement2);
console.log("目标元素的索引是：" + result2);
//随机排列数组（生成随机数组）0

function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}

// 例子
let myArray3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let shuffledArray = shuffleArray(myArray3);
console.log(shuffledArray);